home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / wb / Tripppin.lha / Tripppin / source / jrdobbs.c < prev    next >
C/C++ Source or Header  |  1991-01-10  |  9KB  |  332 lines

  1. /* This handles drawing the pictures (gels) in the game Trippin. */
  2.  
  3.  
  4. #include <exec/memory.h>
  5. #include <graphics/gfx.h>
  6. #include <graphics/sprite.h>
  7. #include <graphics/rastport.h>
  8. #include <graphics/gels.h>
  9. #include <graphics/gfxbase.h>
  10. #include <intuition/intuition.h>
  11. #include "trip.h"
  12.  
  13.  
  14. #define TMPIZE (long) RASSIZE(200, 50)
  15.  
  16.  
  17. import void ShoveMenus();
  18.  
  19. import ubyte board[8][8];
  20.  
  21. import piece oo, bb;
  22.  
  23. import struct Menu manyou;
  24.  
  25.  
  26. struct GfxBase *GfxBase;
  27.  
  28.  
  29. struct NewWindow boardwin = {
  30.     81, 12, 16 * SQIZE + 4 + MARGINWID, 8 * SQIZE + 12, -1, -1,
  31.     MOUSEBUTTONS | MOUSEMOVE | MENUPICK | CLOSEWINDOW,
  32.     REPORTMOUSE | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE
  33.             | SMART_REFRESH | ACTIVATE,
  34.     null, null, (ubyte *) "Tripppin          by Paul Kienitz", null, null,
  35.     0, 0, 0, 0, WBENCHSCREEN
  36. };
  37.  
  38.  
  39. struct Window *win;
  40.  
  41.  
  42. /* image data created by GetImage program from fish 345 */
  43.  
  44. private UWORD ecks[23] = {
  45.     0, 0,
  46.     0, 0x60c0,
  47.     0, 0x71c0,
  48.     0, 0x3b80,
  49.     0, 0x1f00,
  50.     0, 0x0e00,
  51.     0, 0x1f00,
  52.     0, 0x3b80,
  53.     0, 0x71c0,
  54.     0, 0x60c0,
  55.     0, 0, 0
  56. };
  57.  
  58.  
  59. WORD rawimage[IMHITE << 2] = {
  60.     0x0000,0x0000,
  61.     0x00fc,0x0000,
  62.     0x017e,0x0000,
  63.     0x00bc,0x0000,
  64.     0x0058,0x0000,
  65.     0x0038,0x0000,
  66.     0x0078,0x0000,
  67.     0x00bc,0x0000,
  68.     0x057f,0xc000,
  69.     0x2bff,0xf000,
  70.     0x15ff,0xf000,
  71.     0x02b5,0x0000,
  72.     0x0000,0x0000,
  73.  
  74.     0x00fc,0x0000,
  75.     0x03eb,0x0000,
  76.     0x07f5,0x8000,
  77.     0x03fb,0x0000,
  78.     0x01fe,0x0000,
  79.     0x00f4,0x0000,
  80.     0x01f6,0x0000,
  81.     0x0ffb,0xc000,
  82.     0x7ffe,0xb800,
  83.     0xffff,0x6c00,
  84.     0xffff,0xfc00,
  85.     0x7fff,0xf800,
  86.     0x07ff,0x8000
  87. };
  88.  
  89.  
  90. struct Image olabel = {
  91.     0, 0, IMWID, IMHITE, 2, null /* will be oonwhite */, 3, 0, null
  92. }, blabel = {
  93.     0, 0, IMWID, IMHITE, 2, null /* bonwhite */, 3, 0, null
  94. };
  95. /* these are used in the menu and margin stuff, not on the board */
  96.  
  97.  
  98. struct SimpleSprite ox = { &ecks[0], 1, 0, 0, -1 },
  99.             bx = { &ecks[1], 1, 0, 1, -1 };
  100.  
  101.  
  102. extern struct Bob obob, bbob;        /* declared in full below */
  103.  
  104.  
  105. struct VSprite ovs = {
  106.     null, null, null, null, 0, 0,    /* system fields */
  107.     SAVEBACK | OVERLAY,            /* is a Bob */
  108.     0, 0, IMHITE, 2, 2,        /* y, x, height, width (in words), planes */
  109.     0, 0, null,            /* memask, hitmask, image = oimd */
  110.     null, null, null,        /* borderline, collmask = shadow, colormap */
  111.     &obob, 3, 0            /* parent Bob, planepick, planeonoff */
  112. }, bvs = {
  113.     null, null, null, null, 0, 0,    /* system fields */
  114.     SAVEBACK | OVERLAY,            /* is a Bob */
  115.     0, 0, IMHITE, 2, 2,        /* y, x, height, width (in words), planes */
  116.     0, 0, null,            /* memask, hitmask, image = bimd */
  117.     null, null, null,        /* borderline, collmask = shadow, colormap */
  118.     &bbob, 3, 0            /* parent Bob, planepick, planeonoff */
  119. };
  120.  
  121.  
  122. struct VSprite dummy1 = { 0 }, dummy2 = { 0 };
  123.  
  124.  
  125. WORD nextline[8], *(lastcolor[8]);    /* lastcolor is array of pointers */
  126.  
  127.  
  128. struct GelsInfo ginfo = {
  129.     0xFC, 0, null, null, &nextline[0], &lastcolor[0],
  130.     null, 0, 0, 0, 0, null, null
  131. };
  132.  
  133.  
  134. struct /* J.R. " */ Bob /* " Dobbs */ obob = {
  135.     0, null, null, null, null,        /* flags, save, shadow, before, after */
  136.     &ovs, null, null            /* vsprite, animcomp, dbufpacket */
  137. }, bbob = {
  138.     0, null, null, null, null,        /* flags, save, shadow, before, after */
  139.     &bvs, null, null            /* vsprite, animcomp, dbufpacket */
  140. };
  141.  
  142.  
  143. /* pointers into chip ram: */
  144. WORD *oimd, *bimd, *osav, *bsav, *shadow,
  145.     *oonwhite, *bonwhite, *osprat, *bsprat, *nuth;
  146.  
  147.  
  148. short sqite, thite;
  149.  
  150. bool lace;
  151.  
  152. struct RastPort *r;
  153. struct ViewPort *vp;
  154.  
  155. struct TmpRas wtr;
  156. PLANEPTR trp;
  157.  
  158. long imize = 0;
  159.  
  160.  
  161.  
  162. #define TFree(m, s) if (m) FreeMem(m, (long) s)
  163.  
  164. void DumpPrettyPictures()
  165. {
  166.     if (!imize) return;
  167.     TFree(bsprat, 48);
  168.     TFree(osprat, 48);
  169.     TFree(nuth, 24);
  170.     TFree(bonwhite, imize);
  171.     TFree(oonwhite, imize);
  172.     TFree(shadow, imize >> 1);
  173.     TFree(bimd, imize);
  174.     TFree(oimd, imize);
  175.     TFree(bsav, imize << 1);
  176.     TFree(osav, imize << 1);
  177.     if (win) {
  178.     ClearMenuStrip(win);
  179.     CloseWindow(win);
  180.     }
  181.     TFree(trp, TMPIZE);
  182.     if (~ox.num)
  183.     FreeSprite((long) ox.num);
  184.     if (~bx.num)
  185.     FreeSprite((long) bx.num);
  186. }
  187.  
  188.  
  189.  
  190. void MakePrettyPictures()
  191. {
  192.     struct Preferences p;
  193.     short i;
  194.     long *o, *b, *rr = (long *) rawimage;
  195.     ulong rgb;
  196.     struct Screen *wbench = OpenWorkBench();
  197.  
  198.     if (!wbench)
  199.     Die("Workbench inaccessible!\n");
  200.     thite = wbench->Font->ta_YSize;
  201.     /* that's a FAIR GUESS of the height of the font our window will use */
  202.     if (thite < 8 || thite > 40)
  203.     thite = 8;
  204.     GetPrefs(&p, (long) sizeof(p));    /* why put LaceWB down at the end!? */
  205.     lace = p.LaceWB == LACEWB;
  206.     sqite = SQIZE << lace;
  207.     boardwin.Height = thite + 4 + (sqite << 3) + lace;    /* room for title bar */
  208.     boardwin.TopEdge -= thite - 8;        /* constant bottom edge */
  209.     if (boardwin.TopEdge < 0)
  210.     boardwin.TopEdge = 0;
  211.     if (!(win = OpenWindow(&boardwin)))
  212.     Die("Can't open window!\n");
  213.     ShoveMenus();
  214.     SetMenuStrip(win, &manyou);
  215.     if (!(trp = AllocCP(TMPIZE)))
  216.     Die("Can't allocate TmpRas for window!\n");
  217.     InitTmpRas(&wtr, trp, TMPIZE);
  218.     r = win->RPort;
  219.     r->TmpRas = &wtr;
  220.     vp = &win->WScreen->ViewPort;
  221.  
  222.     imize = RASSIZE(IMWID, IMHITE) << (1 + lace);
  223.     /* probably should have used AllocEntry, but it just sorta grew ... */
  224.     if (!(oimd = AllocCP(imize)) || !(bimd = AllocCP(imize)) ||
  225.         !(osav = AllocCP(imize << 1)) ||      /* 16 color WB! */
  226.         !(bsav = AllocCP(imize << 1)) ||
  227.         !(shadow = AllocCP(imize >> 1)) ||
  228.         !(oonwhite = AllocCP(imize)) || !(bonwhite = AllocCP(imize)) ||
  229.         !(osprat = AllocCP(48)) || !(bsprat = AllocCP(48)) ||
  230.         !(nuth = AllocCPZ(24)))
  231.     Die("Can't allocate chip ram for image data!\n");
  232.     o = (long *) (ovs.ImageData = oimd);
  233.     b = (long *) (bvs.ImageData = bimd);
  234.     obob.SaveBuffer = osav;
  235.     bbob.SaveBuffer = bsav;
  236.     obob.ImageShadow = ovs.CollMask = bbob.ImageShadow = bvs.CollMask = shadow;
  237.     ovs.BorderLine = bvs.BorderLine = nuth + 8;
  238.     oo.face = &obob;
  239.     bb.face = &bbob;
  240.     if (lace)
  241.     ovs.Height = bvs.Height = IMHITE << 1;
  242.     for (i = 0; i < IMHITE; i++) {
  243.     register short i1 = i << lace, i2 = (i + IMHITE) << lace;
  244.     register long m, dw;
  245.     m = dw = o[i1] = rr[i];
  246.     if (lace) o[i1 + 1] = dw;        /* double each line for lace */
  247.     m &= (dw = o[i2] = rr[i + IMHITE]);    /* bits on in m are orange */
  248.     if (lace) o[i2 + 1] = dw;
  249.     dw = b[i1] = rr[i] ^ m;        /* ^ m turns orange pixels blue */
  250.     if (lace) b[i1 + 1] = dw;
  251.     dw = b[i2] = rr[i + IMHITE] ^ m;
  252.     if (lace) b[i2 + 1] = dw;
  253.     }
  254.     InitMasks(&ovs);                /* fill in shadow */
  255.     for (i = 0; i < (IMHITE << lace); i++) {
  256.     register short i2 = i + (IMHITE << lace);
  257.     register long m, w;
  258.     m = w = o[i];
  259.     m |= ((long *) oonwhite)[i2] = o[i2];
  260.     ((long *) oonwhite)[i] = w | ~m;    /* turn blue background white */
  261.     ((long *) bonwhite)[i2] = b[i2];
  262.     ((long *) bonwhite)[i] = b[i] | ~m;
  263.     }
  264.     olabel.ImageData = (ushort *) oonwhite;
  265.     blabel.ImageData = (ushort *) bonwhite;
  266.     olabel.Height = blabel.Height = IMHITE << lace;
  267.     ginfo.sprRsrvd &= ~GfxBase->SpriteReserved;
  268.     r->GelsInfo = &ginfo;
  269.     InitGels(&dummy1, &dummy2, &ginfo);
  270.  
  271.     for (i = 0; i < 11; i++) {
  272.     ((long *) osprat)[i] = ((long *) (&ecks[0]))[i];
  273.     ((long *) bsprat)[i] = ((long *) (&ecks[1]))[i];
  274.     }
  275.     for (i = 2; i <= 6; i += 2) {
  276.     if (~GetSprite(&ox, (long) i) && ~GetSprite(&bx, (long) i + 1)) {
  277.         i = (i << 1) + 17;
  278.         rgb = GetRGB4(vp->ColorMap, 0L);    /* "blue" */
  279.         SetRGB4(vp, (long) i, rgb >> 8, (rgb >> 4) & 15, rgb & 15);
  280.         rgb = GetRGB4(vp->ColorMap, 3L);    /* "orange" */
  281.         SetRGB4(vp, i + 1L, rgb >> 8, (rgb >> 4) & 15, rgb & 15);
  282.         return;
  283.     }
  284.     if (~ox.num)
  285.         FreeSprite((long) ox.num);
  286.     if (~bx.num)
  287.         FreeSprite((long) bx.num);
  288.     ox.num = bx.num = -1;
  289.     }
  290.     Die("Can't find a free pair of sprites!\n");
  291. }
  292.  
  293.  
  294.  
  295. void Win2Square(x, y, xp, yp) short x, y, *xp, *yp;
  296. {
  297.     *xp = (x + (SQIZE << 1) - 2) / (SQIZE << 1) - 1;
  298.     *yp = (y + sqite - 2 - thite - lace) / sqite - 1;
  299. }
  300.  
  301.  
  302.  
  303. void DropBob(who) piece *who;
  304. {
  305.     register struct Bob *b = who->face;
  306.     b->BobVSprite->X = who->x * (SQIZE << 1) + 13;
  307.     b->BobVSprite->Y = who->y * sqite + 2 + thite + (5 << lace);
  308.     /* SortGList(r); */
  309.     DrawGList(r, vp);            /* paint it in center of square */
  310.     b->Flags |= SAVEBOB;
  311.     RemIBob(b, r, vp);            /* dump it on the background */
  312. }
  313.  
  314.  
  315.  
  316. void LiftBob(who) piece *who;
  317. {
  318.     who->face->Flags &= ~SAVEBOB;
  319.     AddBob(who->face, r);
  320.     /* SortGList(r); */
  321. }
  322.  
  323.  
  324.  
  325. void DragBob(who, x, y) piece *who; short x, y;
  326. {
  327.     who->face->BobVSprite->X = x - 10;
  328.     who->face->BobVSprite->Y = y - (((IMHITE >> 2) + 3) << lace);
  329.     /* SortGList(r); */
  330.     DrawGList(r, vp);        /* must already be LiftBobbed */
  331. }
  332.